﻿using Direct.IDAL.User.Money;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.User;
using DirectSell.Model.Money;
using System.Collections;

namespace DirectSell.BLL.User.Money
{
    public class UserGiroService:UserService
    {
        #region 日志记录
        /// <summary>
        /// 日志记录文件
        /// </summary>
        protected override string LogFile
        {
            get
            {
                return "内部转账";
            }
        }
        #endregion

        #region 初始化数据
        /// <summary>
        /// 内部转账仓储接口
        /// </summary>
        protected IUserGiroRepository _repository { get; set; }
        public UserGiroService(IUserGiroRepository repository)
        {
            this._repository = repository;
        }
        #endregion

        #region 操作
        /// <summary>
        /// 操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected override UserRespCode Operation<T>(T param)
        {
            var req = param as UserGiroReq;

            if (req == null)
            {
                return UserRespCode.传入参数不正确;
            }

            if (!_repository.CheckSpassword(req.UserCode, req.SecondPassWord))
            {
                return UserRespCode.二级密码输入错误;
            }

            if (!_repository.CheckIsExcits(req.ReceiveCode))
            {
                return UserRespCode.会员编号不存在;
            }

            req.ReceiveName = _repository.GetUserInfo(req.ReceiveCode).UserName;

            Log(req);

            Hashtable hsTable = new Hashtable();
            //电子币互转
            if (req.Type == 0)
            {
                var result = VirtualMoneyGiro(req, ref hsTable);
                if (!result)
                {
                    return UserRespCode.余额不足;
                }
            }
            //提交事务
            if (!_repository.CommitDapperTranLock(hsTable))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;
        }
        #endregion

        #region 电子币互转
        /// <summary>
        /// 电子币互转
        /// </summary>
        /// <param name="req"></param>
        /// <param name="hstable"></param>
        /// <returns></returns>
        protected bool VirtualMoneyGiro(UserGiroReq req,ref Hashtable hstable)
        {
            //判断余额
            if (req.Amount > _repository.QueryVirtualMoneyBanlace(req.UserCode))
            {
                return false;
            }

            Consumption con = new Consumption()
            {
                Affair = "内部转账电子币(支出)",
                Amout = req.Amount,
                CreateDate = req.CreateTime,
                LogType = 0,
                Operator = req.UserCode,
                Relevant = req.ReceiveCode
            };
            Consumption con1 = new Consumption()
            {
                Affair = "内部转账电子币(收入）",
                Amout = req.Amount,
                CreateDate = req.CreateTime,
                LogType = 1,
                Operator = req.ReceiveCode,
                Relevant = req.UserCode
            };

            _repository.InsertConsumption(con, ref hstable);
            _repository.InsertConsumption(con1, ref hstable);
            _repository.InsertGiro(req, ref hstable);
            _repository.Hushmoney(req.UserCode, req.Amount,ref hstable);
            _repository.AddMoney(req.ReceiveCode, req.Amount, ref hstable);
            return true;
        }
        #endregion

    }
}
